package com.littleken.zxing;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.pdf417.decoder.ec.ErrorCorrection;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * 本类用于实现二维码生成
 */
public class CreateQRCode_zxing {
    public static void main(String[] args) {
        int width = 300;
        int height = 300;
        String format = "png";//二维码图片格式
        String content = "com/littleken";//内容


        //通过HasMap存放二维码参数
        HashMap hints = new HashMap();
        //二维码编码格式
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        //设置纠错能力
        hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
        //设置二维码边距
        hints.put(EncodeHintType.MARGIN,2);
        //生成
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content,//内容
                    BarcodeFormat.QR_CODE,//二维码类型
                    width,height,//宽高
                    hints);//二维码设置
            Path file = new File("D://img.png").toPath();

            MatrixToImageWriter.writeToPath(bitMatrix,format,file);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
